public class redirectPage {
    private String Id;

    public redirectPage(ApexPages.StandardController controller) {
        Opportunity oppty = (Opportunity)controller.getRecord();
            Id = oppty.Id;
        bWait = false;
        oppty = [Select StageName, RecordType.Name From Opportunity Where Id = :Id];
        if ((oppty.StageName == 'Listo para pago' || oppty.StageName == 'Application Submitted') && (oppty.RecordType.Name!= null && oppty.RecordType.Name.contains('ULA'))){
            System.debug('----------nyes---');
            bWait = true;
        }
    }
    public Boolean bWait{get; set;}
    
    public PageReference onLoadPage(){
        if (!bWait){
            return redirect();
        }
        else{
            return null;
        }
    }
    public PageReference redirect(){
        PageReference pg = new PageReference('/' + Id + '?nooverride=1');
        pg.setRedirect(true);
        return pg;
    }
    
    public static testmethod void UnitTest1(){
        Campus__c cmps = new Campus__c(Name = 'Unit Test', Active__c = True);
        cmps.recordTypeId = Schema.sObjectType.Campus__c.getRecordTypeInfosByName().get('ULA - Campus').getRecordTypeId();
        insert cmps;
        
        Program__c prgm = new Program__c(Name = 'Unit Test', Active__c = True);
        prgm.recordTypeId = Schema.sObjectType.Program__c.getRecordTypeInfosByName().get('ULA - Traditional Program').getRecordTypeId();
        insert prgm;
        
        Catalog__c ctlg = new Catalog__c(Catalog_Name__c = 'Unit Test', 
                                            Active__c = True, Campus__c = cmps.Id, Program__c = prgm.Id);
        ctlg.X3_Payments__c = true;
        ctlg.X4_Payments__c = true;
        ctlg.X5_Payments__c = true;
        ctlg.X10_Payments__c = true;
        ctlg.X11_Payments__c = true;
        ctlg.X12_Payments__c = true;
        ctlg.Early_Payoff__c = true;
        ctlg.recordTypeId = Schema.sObjectType.Catalog__c.getRecordTypeInfosByName().get('ULA - Traditional Catalog').getRecordTypeId();
        insert ctlg;
        
        Account acc = new Account();
        acc.LastName = 'Test';
        acc.FirstName = 'Unit';
        acc.Account_Assigned_Date__c = Date.valueOf('2012-02-27');
        acc.Age_Rage__pc = '24 to 39';
        acc.Birth_Date__c = Date.valueOf('1982-01-03');
        acc.Delegation_or_Municipality__c = 'Benito Juarez';
        acc.Do_Not_Text_Message__pc = false;
        acc.Exterior_Number__c = '10';
        acc.Extra_curricular_activities__c  = 'Soccer';
        acc.Father_s_Email__c = 'jose@father.com';
        acc.Father_s_First_Name__c = 'Jose';
        acc.Father_s_Last_Name__c = 'Gonzalez';
        acc.Gender__pc = 'Male';
        acc.Other_City__c = 'México DF';
        acc.Other_Postal_Code__c = '03100';
        acc.Other_Street__c = 'Porfirio Díaz';
        acc.Personal_Email__pc   = 'pablo@prueba.com';
        acc.PersonEmail = 'pablo@prueba.com';
        acc.PersonMobilePhone = '5598741230';
        acc.Phone = '5598741230';
        acc.Preferred_Email__pc  = 'Personal Email';
        acc.Preferred_Phone__pc = 'Mobile Phone';
        //acc.RecordTypeId = '012E0000000TgTQ';
        acc.RecordTypeId = Schema.Sobjecttype.Account.getRecordTypeInfosByName().get('ULA - Traditional Student Account').getRecordTypeId();
        acc.Status__c = 'Prospect';
        insert acc;
        
        Opportunity oppty = new Opportunity();
        oppty.Name= 'Unit Test';
        oppty.AccountId = acc.Id;
        //oppty.RecordTypeId = '012E0000000TgTM';
        oppty.RecordTypeId = Schema.Sobjecttype.Opportunity.getRecordTypeInfosByName().get('ULA - Traditional Layout').getRecordTypeId();
        oppty.Registrant_Type__c = 'High School - Local';
        oppty.StageName = 'Application Started';
        oppty.Campus__c = cmps.Id;
        oppty.Program__c = prgm.Id;
        oppty.Catalog__c = ctlg.Id;
        oppty.Payment_plan__c = '3';
        oppty.CloseDate = Date.valueOf('2012-04-01');
        insert oppty;
        
        Test.startTest();
        ApexPages.StandardController sc = new ApexPages.StandardController(oppty);
        redirectPage rp = new redirectPage(sc);
        rp.OnloadPage();
        rp.redirect();
        Test.stopTest();
    }
}